/* SettingsDialog.java
 *
 * Created on 2010-dec-19, 01:59:16
 */

package simplepushserver.gui;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import simplepushserver.SettingsHandler;

/**
 *
 * @author Filip
 */
public class SettingsDialog extends JFrame {
    private SettingsHandler setH;
    private ListSyncer ls;

    public SettingsDialog(SettingsHandler s) {
        this.setH = s;
        ls = new ListSyncer(setH.getMediaFolders());
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new JList(ls);
        exitButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        portField = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        messageField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Settings");

        final JPopupMenu jpop = new JPopupMenu("Context");
        AbstractAction moveUp = new AbstractAction("Move Up") {
            public void actionPerformed(ActionEvent e) {
                int index = jList1.getSelectedIndex();
                ls.moveElementUp(index--);
                jList1.setSelectedIndex(index);
            }
        };
        jpop.add(moveUp);
        AbstractAction moveDown = new AbstractAction("Move Down") {
            public void actionPerformed(ActionEvent e) {
                int index = jList1.getSelectedIndex();
                ls.moveElementDown(index++);
                jList1.setSelectedIndex(index);
            }
        };
        jpop.add(moveDown);

        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addMouseListener(new MouseAdapter() {
             public void mouseClicked(MouseEvent me) {
               // if right mouse button clicked (or me.isPopupTrigger())
               if (SwingUtilities.isRightMouseButton(me)
                   && !jList1.isSelectionEmpty()
                   && jList1.locationToIndex(me.getPoint())
                      == jList1.getSelectedIndex()) {
                       jpop.show(jList1, me.getX(), me.getY());
                       }
                   }
                }
        );


        jScrollPane1.setViewportView(jList1);

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        addButton.setText("Add folder");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete folder");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Port:");

        SpinnerNumberModel snm = new SpinnerNumberModel(setH.getServerPort(), 1024, 49151, 1);
        portField.setModel(snm);

        jLabel2.setText("Mediafolders:");

        jLabel3.setText("Startmessage:");

        messageField.setText(setH.getStartMessage());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 209, Short.MAX_VALUE)
                        .addComponent(exitButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(messageField, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(messageField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exitButton)
                    .addComponent(addButton)
                    .addComponent(deleteButton))
                .addContainerGap())
        );

        pack();
       
    }

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        setH.setStartMessage(messageField.getText().trim());
        try{
            int p = (Integer)portField.getValue();
            if(p!=setH.getServerPort()){
                setH.setPort(p);
            }
            dispose();
        }catch(Exception e) {e.printStackTrace();}
    }

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Select folder...");
        fc.setMultiSelectionEnabled(false);
        fc.setFileSelectionMode(fc.DIRECTORIES_ONLY);
        int ans = fc.showOpenDialog(this);
        if(ans==fc.APPROVE_OPTION){
            if(!setH.getMediaFolders().contains(fc.getSelectedFile())){
                ls.addElement(fc.getSelectedFile());
            }
        }
    }

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
        ls.removeElement(jList1.getSelectedValue());
    }

    private javax.swing.JButton addButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField messageField;
    private javax.swing.JSpinner portField;
    private javax.swing.JButton exitButton;
}
